#include "work_thread.hpp"
#include "events_config.hpp"

#include <unistd.h>

#include <thread>

#include <event2/event.h>
#include <glog/logging.h>
work_thread::work_thread(/* args */)
{
}

work_thread::~work_thread()
{
}
int work_thread::get_pthread_id()
{
    return this->thread_id;
}
void work_thread::set_pthread_id(int pthread_id)
{
    this->thread_id = pthread_id;
}
void work_thread::thread_func()
{
    VLOG(0) << "create  "<<std::this_thread::get_id()<<"  "<<get_pthread_id();
    event_base_dispatch(m_base);
    event_base_free(m_base);
}
void work_thread::start()
{
    setup();
    std::thread threads(&work_thread::thread_func,this);
    threads.detach();
}
void work_thread::setup()
{
    int pipes[2];
    if(pipe(pipes))
    {
        VLOG(3)<<"pipe failed";
        return;
    }
    notify_send_fd = pipes[1];      // 保存写端
    events_config config;
    config.set_event_config();
    auto p = config.get_event_config();
    this->m_base = event_base_new_with_config(p);

    event *ev = event_new(m_base,pipes[0],EV_READ|EV_PERSIST,0,this);

    event_add(ev,0);

    if(!m_base)
    {
        VLOG(3) << "config thread no lock is error";
    }
    return;
}